package com.demo.project_template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.project_template.entity.User;
import com.demo.project_template.entity.UserBlack;
import com.demo.project_template.mapper.UserBlackMapper;
import com.demo.project_template.mapper.UserMapper;
import com.demo.project_template.service.UserBlackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户拉黑记录 服务实现类
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-08-20 11:48:58
 */
@Service
public class UserBlackServiceImpl extends ServiceImpl<UserBlackMapper, UserBlack> implements UserBlackService {
    @Resource
    private UserMapper userMapper;

    @Override
    public IPage<User> handleResult(Page<UserBlack> page) {
        if (page.getRecords() == null || page.getRecords().isEmpty()){
            return null;
        }
        IPage<User> result = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        List<Long> blackUserIds = page.getRecords().stream().map(UserBlack::getBlackUserId).collect(Collectors.toList());
        List<User> blackUsers = userMapper.selectBatchIds(blackUserIds);
        result.setRecords(blackUsers);
        return result;
    }

    @Override
    public void add(UserBlack userBlack) {
        UserBlack black = getOne(new LambdaQueryWrapper<UserBlack>()
                .eq(UserBlack::getUserId, userBlack.getUserId())
                .eq(UserBlack::getBlackUserId, userBlack.getBlackUserId()));
        if (black != null){
            //已有记录说明已经拉黑过了，再次点击为取消拉黑，删除拉黑记录
            removeById(black);
        }else {
            //无记录则走拉黑流程，保存拉黑记录
            save(userBlack);
        }
    }
}
